<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="用户名/电话"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="密码"></wInput>
			</view>
			<wButton class="wbutton" text="登 录" :rotate="isRotate" @click="startLogin"></wButton>
			<!-- 底部信息 -->
			<view class="footer">
				<p>© 2020 <a href="#" target="_blank">重庆蓝星云科技有限责任公司</a></p>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: 'admin', //用户/电话
				passData: 'zcg2018', //密码
				isRotate: false, //是否加载旋转
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		methods: {
			isLogin() {

			},
			startLogin(e) {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return;
				}

				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
				uni.showLoading({
					title: '登录中'
				});
				var baseServer = 'http://192.168.0.110/zsjmrms/loginIm';
				// var baseServer = '/zsjmrms/loginIm';
				uni.request({
					method: 'POST',
					url: baseServer, //仅为示例，并非真实接口地址。
					sslVerify: false,
					data: {
						'username': _this.phoneData,
						'password': _this.passData
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
					},
					success: (res) => {
						if (res.data.code == 200) {
							uni.showToast({
								title: '登录成功'
							});
							// 本地存储
							uni.setStorageSync("name", _this.phoneData);
							uni.setStorageSync("token", res.data.data);
							setTimeout(function(res) {
								uni.navigateTo({
									url: '../tabbar-1/index'
								});
							}, 1500)
						} else {
							uni.showToast({
								icon: 'none',
								position: 'bottom',
								title: res.data.message
							});
						}
						uni.hideLoading();
					},
					fail: function() {
						uni.hideLoading();
					}
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
